package com.pagepub.core.tag.validator;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

public class ValidateSubmit extends FormValidateBasic {
	/**
	 * 
	 */
	private static final long serialVersionUID = -3990979403176695211L;

	StringBuffer str = new StringBuffer();

	/**
	 * @param args
	 */

	private String submitform;

	private String initstr;

	public String getInitstr() {
		return initstr;
	}

	public void setInitstr(String initstr) {
		this.initstr = initstr;
	}

	public static void main(String[] args) {
		// TODO 自动生成方法存根

	}

	@Override
	public int doStartTag() throws JspException {
		JspWriter out = this.pageContext.getOut();
		validatesubmit();
		// docheckstatus();
		try {
			out.print(str);
			// System.out.println(str);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return 1;
	}

	public void validatesubmit() {
		String initstring[] = null;
		if (this.getInitstr() != null
				&& !"".equalsIgnoreCase(this.getInitstr()))
			initstring = initstr.trim().split(",");
		String str0 = "";
		if (initstring != null && initstring.length > 0) {
			for (int i = 0; i < initstring.length; i++) {
				if (i != 0)
					str0 = str0 + ",\"" + initstring[i] + "\"";

				else if (i == 0)
					str0 = "\"" + initstring[i] + "\"";
			}
		}
		// System.out.println(str0);
		str.append("<script language=\"javascript\">");
		if (str0 != null && !"".equalsIgnoreCase(str0))
			str.append(" var submitcheckarray=new Array(" + str0 + ");");
		else
			str.append(" var submitcheckarray=new Array();");
		str.append("function checksubmit(spanname){");
		str.append("  if(submitcheckarray.length>0){");
		str.append("	message=\"对不起，\"+submitcheckarray[0]+\"不能为空或操作有误！\";");
		str.append("	imgurl=\"<img src=\\\"" + wrongimage + "\\\"/>\";	");
		str.append(this.display());
		str.append("return false;");
		str.append(" }");
		str.append(" else{");
		str.append(this.displayempty());
		str.append(" document." + submitform + ".submit();");
		str.append(" }");
		str.append("}");
		emptyelement();
		pluselement();
		str.append("</script>");
		this.cTrim();

	}

	public void emptyelement() {

		str.append("function emptyelement(str){");
		str.append("var emptyflag=0;");

		str.append("var changarray=new Array();");
		str.append("for(var t=0;t<submitcheckarray.length;t++){");
		str.append(" if(cTrim(submitcheckarray[t])!=cTrim(str)){");
		str.append(" changarray[emptyflag]=submitcheckarray[t];");
		str.append(" emptyflag++;");
		str.append("}");
		str.append("}");
		// str.append("alert(changarray);");
		str.append("submitcheckarray=changarray;");
		// str.append("alert(submitcheckarray);");
		str.append("}");

	}

	public void pluselement() {

		str.append("function pluselement(str){");
		str.append("var emptyflag=0;");

		str.append("var changarray=new Array();");
		str.append(" changarray[emptyflag]=cTrim(str);");
		str.append(" emptyflag++;");
		str.append("for(var t=0;t<submitcheckarray.length;t++){");
		str.append(" changarray[emptyflag]=submitcheckarray[t];");
		str.append(" emptyflag++;");
		str.append("}");
		// str.append("alert(changarray);");
		str.append("submitcheckarray=changarray;");
		// str.append("alert(submitcheckarray);");
		str.append("}");

	}

	public void docheckstatus() {
		str.append("<script language=\"javascript\">");
		str.append("var checksubmitflag=false;");
		str.append("var submitcheckflag=false;");
		str.append("function dilivery(nextcheckname){");
		str.append("if(submitcheckflag==true) {");
		str.append("alert(nextcheckname);");
		str
				.append("  var tempname=document.getElementsByName(nextcheckname)[0];");
		str.append(" tempname.focus();");
		str.append(" tempname.blur();");
		str.append("dosubmit();");

		str.append("if(nextcheckname==\"end\") {");
		str.append("checksubmitflag=true;");
		str.append("dosubmit();");
		str.append(" }");
		str.append("   }");
		str.append(" }");
		str.append(" function dosubmit(){");
		str.append("  if(checksubmitflag==true){");
		str.append("alert(checksubmitflag);");
		str.append("  document." + this.submitform + ".submit();");
		str.append(" }");
		str.append("else {");
		str.append("	message=\"对不起,填写有误!不能提交数据.\";");
		str.append("	imgurl=\"<img src=\\\"" + wrongimage + "\\\"/>\";	");
		str.append(this.display());
		str.append("}");
		str.append("}");
		str.append(" function checksubmit(firstcheckname,spanname){");
		str.append("checksubmitflag=false;");
		str.append("submitcheckflag=true;");
		str.append(" dilivery(firstcheckname);");
		str.append("}");
		str.append("</script>");
	}

	public String getSubmitform() {
		return submitform;
	}

	public void setSubmitform(String submitform) {
		this.submitform = submitform;
	}
}
